import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.ivideo_2211a.R
import com.example.ivideo_2211a.databinding.ItemVideoBinding
import com.example.ivideo_2211a.home.HomeData
import com.example.ivideo_2211a.fyt.FytListData

class FytVideoAdapter(private val onVideoClick: (HomeData) -> Unit = {}) : ListAdapter<FytListData, FytVideoAdapter.VideoViewHolder>(VideoDiffCallback()) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VideoViewHolder {
        val binding: ItemVideoBinding = DataBindingUtil.inflate(
            LayoutInflater.from(parent.context),
            R.layout.item_video,
            parent,
            false
        )
        return VideoViewHolder(binding, onVideoClick)
    }

    override fun onBindViewHolder(holder: VideoViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    class VideoViewHolder(
        private val binding: ItemVideoBinding,
        private val onVideoClick: (HomeData) -> Unit
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(video: FytListData) {
            // 转换为HomeData以兼容UI，publish_time为null时兜底
            val safePublishTime = video.publish_time ?: ""
            val homeData = HomeData(
                avatar_url = video.avatar_url,
                channelid = video.channelid,
                commentnum = video.commentnum,
                ctime = video.ctime,
                description = video.description,
                group_id = video.group_id,
                id = video.id,
                image_url = video.image_url,
                item_id = video.item_id,
                labelIds = video.labelIds,
                name = video.name,
                playnum = video.playnum,
                preview_url = video.preview_url,
                publish_time = safePublishTime,
                title = video.title,
                userid = video.userid,
                verifycode = video.verifycode,
                videomainimag = video.videomainimag,
                videopath = video.videopath
            )
            binding.video = homeData
            binding.executePendingBindings()
            binding.root.setOnClickListener {
                onVideoClick(homeData)
            }
            Glide.with(binding.root.context)
                .load(homeData.videomainimag)
                .into(binding.videoThumbnail)
            Glide.with(binding.root.context)
                .load(homeData.avatar_url)
                .into(binding.userAvatar)
        }
    }

    class VideoDiffCallback : DiffUtil.ItemCallback<FytListData>() {
        override fun areItemsTheSame(oldItem: FytListData, newItem: FytListData): Boolean {
            return oldItem.id == newItem.id
        }
        override fun areContentsTheSame(oldItem: FytListData, newItem: FytListData): Boolean {
            return oldItem == newItem
        }
    }
}
